// pages/cart/cart.js
var app = getApp();
var x_start, x;

function makeid() {
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    for (var i = 0; i < 10; i++)
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    return text;
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        check_all: 1,
        total_price: 0,
        goods: [{
            select: 1, //是否选中，0不选中
            position: 0, //删除按钮位置
            img: 'http://img95.699pic.com/photo/00013/9236.jpg_wh300.jpg',
            brand: '品牌',
            name: '商品名称商品名称名称称称称称称称称称称称',
            price: 998.00,
            guige: [{
                name: '颜色',
                value: '黑色'
            }, {
                name: '尺码',
                value: '均码'
            }],
            num: 4,
            id: 555
        }, {
            select: 1, //是否选中，0不选中
            position: 0, //删除按钮位置
            img: 'http://img95.699pic.com/photo/00013/9236.jpg_wh300.jpg',
            brand: '品牌',
            name: '商品名称商品名称名称称称称称称称称称称称',
            price: 998.00,
            guige: [{
                name: '颜色',
                value: '黑色'
            }, {
                name: '尺码',
                value: '均码'
            }],
            num: 4,
            id: 555
        }]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var arr = [1, 2, 3, 4, 5, 6, 7, 8];
        app.getUrl(options);
        console.log(arr);
        var that = this;
        //   app.getUrl(options);
        that.setData({
            total_price: 0,
        })
        for (var i in that.data.goods) {
            if (that.data.goods[i].select == 0) {
                that.setData({
                    check_all: 0
                })
            } else {
                that.setData({
                    total_price: that.data.total_price += that.data.goods[i].price
                })
            }
        }

    },
    /**
     * 调用全局返回函数 
     */
    returnPage: function(e) {
        //   app.returnPage(e);
    },
    /**
     * 
     */
    slidestart: function(e) {

        x_start = e.touches[0].clientX
        x = x_start
    },
    /**
     * 滑动
     */
    slide: function(e) {
        var that = this;

        if (e.touches[0].clientX < x) {
            that.data.goods[e.currentTarget.dataset.index].position = -60;
        } else {
            that.data.goods[e.currentTarget.dataset.index].position = 0;
        }
        that.setData({
            goods: that.data.goods
        })
    },
    /**
     * 商品选中事件
     */
    select: function(e) {
        var that = this;

        if (that.data.goods[e.currentTarget.dataset.id].select == 1) {
            that.data.goods[e.currentTarget.dataset.id].select = 0
        } else {
            that.data.goods[e.currentTarget.dataset.id].select = 1
        }
        that.setData({
            check_all: 1,
            total_price: 0
        })
        for (var i in that.data.goods) {
            if (that.data.goods[i].select == 0) {
                that.setData({
                    check_all: 0
                })
            } else {
                that.setData({
                    total_price: that.data.total_price += that.data.goods[i].price
                })
            }
        }
        that.setData({
            goods: that.data.goods
        })
    },
    /**
     * 全选事件
     */
    check_all: function(e) {
        var that = this;
        that.setData({
            total_price: 0
        })
        if (e.currentTarget.dataset.select == 1) {
            that.setData({
                check_all: 0
            })
            for (var i in that.data.goods) {
                that.data.goods[i].select = 0
            }
        } else {
            that.setData({
                check_all: 1
            })
            for (var i in that.data.goods) {
                that.data.goods[i].select = 1
            }
        }
        for (var i in that.data.goods) {
            if (that.data.goods[i].select == 1) {
                that.setData({
                    total_price: that.data.total_price += that.data.goods[i].price
                })
            }
        }
        that.setData({
            goods: that.data.goods
        })
    },
    /**
     * 调用全局返回函数 
     */
    returnPage: function(e) {
        app.returnPage(e);
    }
})